<?php
require('../includes/class.main.php');
require('../includes/admin.functions.php');

$manager = new MeetingManager();

chdir('../bin');
$file_name = "meetings_export-" . date("YmdHis") . ".xls";
$perlcmd = Props::get("exec", "perl");

exec($perlcmd . ' ./MeetingDataExport.pl ../xlsdata/' . $file_name, $output, $return);

if ($return != 0) {
	$result = "<font color=\"red\">ERROR: Database Export Failed </font>\n";
	$result .= "An error was encountered during processing. \n\n";
		
	foreach ($output as $line) {
		echo $line."\n";
		$result .= $line."\n";
	}

} else {

	$result .= "The " . $file_name . " export has been successfuly created.  \n\n";

	// remove old files - only keep 5 newest
	$files = sortedDirectoryList('../xlsdata/', '/^meetings_export-.*\.xls$/');

	while (count($files) > 5) {
		$ent = array_pop($files);
		unlink('../xlsdata/' . $ent['name']);
	}
}

chdir('../admin');

$_SESSION['exportStatus'] = $result;


header('Location: export-meetings.php', true, 303);

?>